home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagn_r.zip / NUMBERS.SWG / 0002_BITS2.PAS.pas < prev    next >
Pascal/Delphi Source File  |  1993-05-28  |  3KB  |  105 lines

  1. {
  2. ROB GREEN
  3.  
  4. > What if I want to just access a bit?  Say I have a Byte, to store
  5. > Various access levels (if it does/doesn't have this, that, or the
  6. > other).  How can I
  7. > 1)  Access, say, bit 4?
  8. > 2)  Give, say, bit 4, a value of 1?
  9.  
  10. Heres a Procedure i wrote to handle all that.  if you need speed, then
  11. i suggest to manually check each bit, rather than use the Procedures.
  12.  
  13. (these Procedures are based on 1, not 0.  thus each Byte is like so:
  14. 87654321   instead of 76543210.  to change to 0 base, change the Array to
  15. [0..31] instead of [1..32].)
  16.  
  17. to set a bit: (b is an Integer Type, BIT is which bit to set
  18.    b:=b or BIT;   ex: b:=b or 128  (set bit 8)
  19.  
  20. to clear a bit:
  21.    b:=b and not BIT;  ex:b:=b and not 8;  (clears bit 4)
  22.  
  23. to check a bit:
  24.    if b and BIT<>0 then..  ex:if b and 64 then..  (check bit 7)
  25. }
  26.  
  27. Const
  28. { Used to convert the Bit value to the actual corresponding number }
  29.    bit : Array[1..32] of LongInt =
  30.        (1, 2, 4, 8, $10, $20, $40, $80, $100, $200, $400, $800, $1000, $2000,
  31.         $4000, $8000, $10000, $20000, $40000, $80000, $100000, $200000,
  32.         $400000, $800000, $1000000, $2000000, $4000000, $8000000, $10000000,
  33.         $20000000, $40000000, $80000000);
  34.  
  35. {b is which bit to set(1-32), size is the size of temp.
  36. Use  SIZEOF(TEMP) to get the value, and temp is the actuall Integer based
  37. number
  38. returns True if bit set, False if not}
  39.  
  40. Function checkbit(b : Byte; size : Byte; Var temp) : Boolean; {1-32}
  41. Var
  42.   c : Boolean;
  43. begin
  44.    c:=False;
  45.    Case size of
  46.      1 : c := Byte(temp) and bit[b] <> 0;     {Byte,shortint}
  47.      2 : c := Word(temp) and bit[b] <> 0;     {Word,Integer}
  48.      4 : c := LongInt(temp) and bit[b] <> 0;  {LongInt}
  49.      else
  50.        Writeln('Invalid size');
  51.    end;
  52.    checkbit := c;
  53. end;
  54.  
  55. {b,size,and temp same as above.  if onoff =True the bit will be set,
  56. else the bit will be cleared}
  57.  
  58. Procedure setbit(b : Byte; onoff : Boolean; size : Byte; Var temp); {1-32}
  59. begin
  60.    if onoff then
  61.    Case size of
  62.      1 : Byte(temp) := Byte(temp) or bit[b];        {Byte}
  63.      2 : Word(temp) := Word(temp) or bit[b];        {Word}
  64.      4 : LongInt(temp) := LongInt(Temp) or bit[b];  {LongInt}
  65.      else
  66.        Writeln('Invalid size');
  67.    end
  68.    else
  69.    Case size of
  70.      1 : Byte(temp) := Byte(temp) and not bit[b];   {Byte}
  71.      2 : Word(temp) := Word(temp) and not bit[b];   {Word}
  72.      4 : LongInt(temp) := LongInt(Temp) and not bit[b];{LongInt}
  73.      else
  74.        Writeln('Invalid size');
  75.    end;
  76. end;
  77.  
  78. {this is a sample test Program i wrote For you to see how to use the
  79. stuff above}
  80.  
  81. Var
  82.   i : LongInt;
  83.   j : Byte;
  84. begin
  85.    i := 0;
  86.    setbit(4,True,sizeof(i),i);  {8}
  87.    Writeln(i);
  88.    setbit(9,True,sizeof(i),i);  {256+8 = 264}
  89.    Writeln(i);
  90.    setbit(9,False,sizeof(i),i); {8}
  91.    Writeln(i);
  92.    setbit(20,True,sizeof(i),i); { $80000+8 = $80008}
  93.    Writeln(i);
  94.    For i := 65550 to 65575 do
  95.    begin
  96.      Write(i : 8, ' = ');
  97.      For j := 32 downto 1 do {to print right}
  98.        if checkbit(j, sizeof(i), i) then
  99.          Write('1')
  100.        else
  101.          Write('0');
  102.      Writeln;
  103.    end;
  104. end.
  105.